package com.penghaisoft.microwms.material.web.outwarehouse.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.RawRequisitionSn;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IMesInterfaceService;
import com.penghaisoft.microwms.common.service.MesProperties;
import com.penghaisoft.microwms.common.wms.model.RawBack;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.material.web.outwarehouse.service.IMaterialOutService;

/**
 * @program: micro-wms
 * @description: 原材料出库demo
 * @author: zhangx
 * @create: 2018-05-08 09:34
 **/
@RestController
@RequestMapping("outwarehouse/rawback")
public class RawBackController extends BaseController {

    @Autowired
    private IMaterialOutService materialOutService;
    
    @Resource
	private MesProperties mesProperties;
    
    @Autowired
	private IMesInterfaceService mesInterfaceService;

    @PostMapping("list")
    public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, RawBack rawBack) {

        Pager<RawBack> result = materialOutService.findRawBackListByCondition(page, rows, rawBack);
        
        String sumInfo = result.getOrder();
		if (sumInfo != null && sumInfo.length() > 0) {
			result.setOrder(sumInfo);
		}
		
        return success(result);
    }

    /** 
    * @Title: postRawStorageSnByCode 
    * @Description: 原材料 退料 查询中  修改退料PE管长度 按钮的验证
    * @param @param rawBack
    * @param @return  参数说明 
    * @return TransResult    返回类型
    * @author luoteng 
    * @date 2018年10月10日 上午9:49:33 
    * @throws 
    */
    @PostMapping("r/detail")
    public TransResult postRawStorageSnByCode(RawBack rawBack){
    	TransResult result = new TransResult();
    	result.setCode(Constant.SUCCESS);
    	
    	RawBack tmp1 = new RawBack();
    	tmp1.setBackId(rawBack.getBackId());
        List<RawBack> cmpList1 = materialOutService.queryRawBackList(tmp1);
        if(cmpList1 == null || cmpList1.isEmpty()){
        	result.setCode(Constant.FAIL);
        	return result;
        }
        Date nowModiBarTime = cmpList1.get(0).getGmtCreate();
        
        RawBack tmp2 = new RawBack();
    	tmp2.setBarcode(rawBack.getBarcode());
        List<RawBack> cmpList2 = materialOutService.queryRawBackList(tmp2);
        if(cmpList2 == null || cmpList2.isEmpty()){
        	result.setCode(Constant.FAIL);
        	return result;
        }
        
        for(RawBack tmp : cmpList2){
        	Date dateTmp = cmpList2.get(0).getGmtCreate();
        	if(nowModiBarTime.before(dateTmp)){
        		result.setCode(Constant.FAIL);
        		result.setMsg("请选择当前条码的最后一次退料记录进行修改！");
            	return result;
        	}
        }
        
        RawStorageSn rawStorageSn = new RawStorageSn();
    	rawStorageSn.setBarcode(rawBack.getBarcode());
    	rawStorageSn.setActiveFlag("1");
    	List<RawStorageSn> list = materialOutService.queryRawStorageSnList(rawStorageSn);
    	if(list == null || list.isEmpty() || list.size() > 1){
    		result.setCode(Constant.FAIL);
    		result.setMsg("该条码库存记录存在问题！");
    		return result;
    	}else{
    		RawStorageSn tmp = list.get(0);
//    		0可用；1 冻结；2 已出库
    		if("1".equals(tmp.getBarcodeStatus())){
    			result.setCode(Constant.FAIL);
        		result.setMsg("该条码已经冻结！");
        		return result;
    		}else if("2".equals(tmp.getBarcodeStatus())){
    			result.setCode(Constant.FAIL);
        		result.setMsg("该条码已经出库！");
        		return result;
    		}
    	}
        
        result.setData(cmpList1);
        
        return result;
    }
    
    /** 
    * @Title: updatePE 
    * @Description: 修改最后一次退料的pe管的长度
    * @param @param rawBack
    * @param @return  参数说明 
    * @return TransResult    返回类型
    * @author luoteng 
    * @date 2018年10月9日 下午4:37:12 
    * @throws 
    */
    @PostMapping("updatePE")
	public TransResult updatePE(RawBack rawBack){
    	TransResult result = new TransResult();
    	result.setCode(Constant.SUCCESS);
    	
    	RawBack tmp1 = new RawBack();
    	tmp1.setBackId(rawBack.getBackId());
        List<RawBack> cmpList1 = materialOutService.queryRawBackList(tmp1);
        if(cmpList1 == null || cmpList1.isEmpty()){
        	result.setCode(Constant.FAIL);
        	return result;
        }
        
        RawBack compareOb = cmpList1.get(0);
        Date nowModiBarTime = compareOb.getGmtCreate();
        
        RawBack tmp2 = new RawBack();
    	tmp2.setBarcode(rawBack.getBarcode());
        List<RawBack> cmpList2 = materialOutService.queryRawBackList(tmp2);
        if(cmpList2 == null || cmpList2.isEmpty()){
        	result.setCode(Constant.FAIL);
        	return result;
        }
        
        for(RawBack tmp : cmpList2){
        	Date dateTmp = cmpList2.get(0).getGmtCreate();
        	if(nowModiBarTime.before(dateTmp)){
        		result.setCode(Constant.FAIL);
        		result.setMsg("请选择当前条码的最后一次退料记录进行修改！");
        	}
        }
        
        Date now = new Date();
        rawBack.setLastModifiedBy(getLoginName());
        rawBack.setGmtModified(now);
        
//        退料长度修改的校验
        result = materialOutService.updateBackCheck(rawBack);
        if(result.getCode().equals(Constant.FAIL)) {
        	return result;
        }
        
        if(rawBack.getUserDefined1() != null && rawBack.getUserDefined1().equals("手工出库退料")) {
        	result = materialOutService.updateSGBack(rawBack);
        }else {
        	String url = mesProperties.getUrl() + "/imes-provider/productplan/follow/ordersplit/back";
            
            JSONObject ob = new JSONObject();
            ob.put("requisitionNo", compareOb.getUserDefined1());
            ob.put("requisitionType", compareOb.getUserDefined2());
            ob.put("model", compareOb.getModel());
            ob.put("modelType", compareOb.getModelType());
            ob.put("barcode", compareOb.getBarcode());
            ob.put("backAmount", rawBack.getAmount());
//    		调mes新增接口
    		HandResp handResp = mesInterfaceService.publicWSResp(url, MediaType.APPLICATION_JSON_TYPE, ob.toJSONString());
    		if(handResp.getCode() != null && handResp.getCode().equals("0")){
    			result.setCode(handResp.getCode());
    			result.setMsg(handResp.getMsg());
    	        return result;
    		}else{
    			result = materialOutService.updateBack(rawBack);
    		}
        }
        
        return result;
    }
}
